package com.ah.shanxi_demo.controller.web;

import com.ah.shanxi_demo.entity.SxRegister;
import com.ah.shanxi_demo.param.input.RegisterPO;
import com.ah.shanxi_demo.param.result.RegisterRO;
import com.ah.shanxi_demo.param.result.SXResult;
import com.ah.shanxi_demo.service.IRegisterService;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import static com.ah.shanxi_demo.convert.ConvertRegister.convertParam;


/**
 * 山东定制项目
 *
 * @author zt 2020/5/12
 */
@RestController
@Api(value = "陕西高速", description = "陕西高速-注册同步服务")
@RequestMapping(value = "/api/v1/shanxi/")
public class ApiRegisterController {

    private static final Logger log = LoggerFactory.getLogger(ApiRegisterController.class);

    @Resource
    private IRegisterService registerService;

    /**
     * 注册接口
     */
    @PostMapping(value = "register", produces = "application/json;charset=UTF-8")
    public SXResult<RegisterRO> nodesRegister(@RequestBody RegisterPO registerPO) {
        SxRegister sxRegister = convertParam(registerPO);
        SxRegister insert = new SxRegister();
        try {
            insert = registerService.insert(sxRegister);
        } catch (Throwable e) {
            log.error("ApiRegisterController 【nodesRegister】" + e.getMessage());
        }
        return SXResult.instance(insert);
    }

    /**
     * 查询注册信息
     */
    @PostMapping(value = "register/show", produces = "application/json;charset=UTF-8")
    public SXResult<String> registerShow() {
        SxRegister insert = new SxRegister();
        try {
            insert = registerService.queryRecent();
        } catch (Throwable e) {
            log.error("ApiRegisterController 【registerShow】" + e.getMessage());
        }
        return SXResult.instance(insert);
    }

    /**
     * session测试
     */
    @PostMapping(value = "session", produces = "application/json;charset=UTF-8")
    public SXResult<String> session(HttpServletResponse response, HttpServletRequest request) {
        HttpSession session = request.getSession();
        session.setAttribute("sx_center_code", "sx_center_code");
        session.setAttribute("sx_center_key", "sx_center_key");
        session.setAttribute("sx_center_key", "sx_center_key_second");

        System.out.println(session.getAttribute("sx_center_key"));


        return SXResult.instance(session.getAttribute("sx_center_key"));
    }

}
